package com.cutefool.zero.code.core.domain;

import com.cutefool.dsl.mybatis.core.objects.BasicsDomain;
import com.fasterxml.jackson.annotation.JsonAlias;
import jakarta.annotation.Generated;
import java.sql.JDBCType;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.UnaryOperator;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateModel;

@Data
@EqualsAndHashCode(callSuper = true)
public class ModelColumnOption extends BasicsDomain<ModelColumnOption> {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("MODEL_ID_")
    private Long modelId;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("COLUMN_ID_")
    private Long columnId;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("DBMS_ID_")
    private Long dbmsId;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("APP_ID_")
    private Long appId;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("COMPANY_ID_")
    private Long companyId;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("OPTION_")
    private String option;

    private static final ModelColumnOption instance = new ModelColumnOption();

    private static final Table table = new Table();

    public static ModelColumnOption instance() {
        return instance;
    }

    @Override
    public Table table() {
        return table;
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<InsertDSL<ModelColumnOption>> insert() {
        return (InsertDSL<ModelColumnOption> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.modelId).toProperty("modelId");
            dsl.map(table.columnId).toProperty("columnId");
            dsl.map(table.dbmsId).toProperty("dbmsId");
            dsl.map(table.appId).toProperty("appId");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            dsl.map(table.companyId).toProperty("companyId");
            dsl.map(table.option).toProperty("option");
            return dsl;
        };
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<MultiRowInsertDSL<ModelColumnOption>> insertMultiple() {
        return (MultiRowInsertDSL<ModelColumnOption> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.modelId).toProperty("modelId");
            dsl.map(table.columnId).toProperty("columnId");
            dsl.map(table.dbmsId).toProperty("dbmsId");
            dsl.map(table.appId).toProperty("appId");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            dsl.map(table.companyId).toProperty("companyId");
            dsl.map(table.option).toProperty("option");
            return dsl;
        };
    }

    @Override
    public void updateAllColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.modelId).equalTo(this::getModelId)
           .set(table.columnId).equalTo(this::getColumnId)
           .set(table.dbmsId).equalTo(this::getDbmsId)
           .set(table.appId).equalTo(this::getAppId)
           .set(table.createBy).equalTo(this::getCreateBy)
           .set(table.createAt).equalTo(this::getCreateAt)
           .set(table.updateBy).equalTo(this::getUpdateBy)
           .set(table.updateAt).equalTo(this::getUpdateAt)
           .set(table.companyId).equalTo(this::getCompanyId)
           .set(table.option).equalTo(this::getOption);
        if(Objects.nonNull(getId())) {
            dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
        }
    }

    @Override
    public void updateSelectiveColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.modelId).equalToWhenPresent(this::getModelId)
           .set(table.columnId).equalToWhenPresent(this::getColumnId)
           .set(table.dbmsId).equalToWhenPresent(this::getDbmsId)
           .set(table.appId).equalToWhenPresent(this::getAppId)
           .set(table.createBy).equalToWhenPresent(this::getCreateBy)
           .set(table.createAt).equalToWhenPresent(this::getCreateAt)
           .set(table.updateBy).equalToWhenPresent(this::getUpdateBy)
           .set(table.updateAt).equalToWhenPresent(this::getUpdateAt)
           .set(table.companyId).equalToWhenPresent(this::getCompanyId)
           .set(table.option).equalToWhenPresent(this::getOption);
        if(Objects.nonNull(getId())) {
            dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
        }
    }

    @Override
    @SuppressWarnings("all")
    public SqlColumn[] columns() {
        return new SqlColumn[]{table.id, table.modelId, table.columnId, table.dbmsId, table.appId, table.createBy, table.createAt, table.updateBy, table.updateAt, table.companyId, table.option};
    }

    @Override
    public Map<String, SqlColumn<?>> columnsMap() {
        Map<String, SqlColumn<?>> maps = new LinkedHashMap<>(11);
        maps.put("id", table.id);
        maps.put("modelId", table.modelId);
        maps.put("columnId", table.columnId);
        maps.put("dbmsId", table.dbmsId);
        maps.put("appId", table.appId);
        maps.put("createBy", table.createBy);
        maps.put("createAt", table.createAt);
        maps.put("updateBy", table.updateBy);
        maps.put("updateAt", table.updateAt);
        maps.put("companyId", table.companyId);
        maps.put("option", table.option);
        return maps;
    }

    @Override
    public SqlColumn<Long> id() {
        return table.id;
    }

    public static final class Table extends org.mybatis.dynamic.sql.AliasableSqlTable<Table> {
        public final SqlColumn<Long> id = column("ID_", JDBCType.BIGINT);

        public final SqlColumn<Long> modelId = column("MODEL_ID_", JDBCType.BIGINT);

        public final SqlColumn<Long> columnId = column("COLUMN_ID_", JDBCType.BIGINT);

        public final SqlColumn<Long> dbmsId = column("DBMS_ID_", JDBCType.BIGINT);

        public final SqlColumn<Long> appId = column("APP_ID_", JDBCType.BIGINT);

        public final SqlColumn<Long> createBy = column("CREATE_BY_", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> createAt = column("CREATE_AT_", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> updateBy = column("UPDATE_BY_", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> updateAt = column("UPDATE_AT_", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> companyId = column("COMPANY_ID_", JDBCType.BIGINT);

        public final SqlColumn<String> option = column("OPTION_", JDBCType.LONGVARCHAR);

        public Table() {
            super("CUTE_MODEL_COLUMN_OPTION", Table::new);
        }
    }
}